const db = require('../config/database');

class Like {
    static async create(likeData) {
        const { news_id, user_id } = likeData;
        const sql = `
            INSERT INTO likes (news_id, user_id, created_at)
            VALUES (?, ?, NOW())
        `;
        const [result] = await db.query(sql, [news_id, user_id]);
        return result.insertId;
    }

    static async delete(likeData) {
        const { news_id, user_id } = likeData;
        const sql = `
            DELETE FROM likes 
            WHERE news_id = ? AND user_id = ?
        `;
        const [result] = await db.query(sql, [news_id, user_id]);
        return result.affectedRows > 0;
    }

    static async exists(likeData) {
        const { news_id, user_id } = likeData;
        const sql = `
            SELECT COUNT(*) as count 
            FROM likes 
            WHERE news_id = ? AND user_id = ?
        `;
        const [rows] = await db.query(sql, [news_id, user_id]);
        return rows[0].count > 0;
    }

    static async getNewsLikesCount(newsId) {
        const sql = `
            SELECT COUNT(*) as count 
            FROM likes 
            WHERE news_id = ?
        `;
        const [rows] = await db.query(sql, [newsId]);
        return rows[0].count;
    }
}
//11
module.exports = Like; 